package com.jingdianjichi.oss.controller;

import com.jingdianjichi.oss.adapter.FileService;
import com.jingdianjichi.oss.adapter.StorageAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
public class FileController {

    @Resource
    private FileService fileService;

    @GetMapping("/test")
    public String test() throws Exception {
        List<String> allBuckets = fileService.getAllBuckets();
        return allBuckets.toString();
    }

    @GetMapping("/getUrl")
    public String getUrl(String bucketName, String fileName) throws Exception {

        return fileService.getUrl(bucketName,fileName);
    }

    @PostMapping("/upload")
    public String upload(MultipartFile file,
                         @RequestParam("bucketName") String bucketName,
                         @RequestParam("fileName") String fileName) throws Exception {
        log.info("bucketName:[{}]   fileName:[{}]",bucketName,fileName);
        //fileName = fileName+"/"+file.getOriginalFilename();
        fileName = fileName+"/"+file.getOriginalFilename();
        log.info("fileName:[{}]",fileName);
        //                          bucketName/fileName
        //http://192.168.10.10:9000/jichi/bubu/微信图片_20250520174945.jpg
        String url = fileService.uploadFile(file,bucketName,fileName);
        return url;
    }
}
